package com.kly.user.task.v4;

import cn.hutool.core.net.NetUtil;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.kly.user.db.dao.StatsDailyModelLikeRatesDao;
import com.kly.user.db.dao.TrafficModelPoolDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;

/**
 * 创建model池
 */
@Component
@Slf4j
public class CreateModelPoolTask {

    @Autowired
    private StatsDailyModelLikeRatesDao statsDailyModelLikeRatesDao;

    @Autowired
    private TrafficModelPoolDao trafficModelPoolDao;

    private String localIP = NetUtil.getLocalhostStr();

    @NacosValue(value = "${master.task.job.executor.ip}", autoRefreshed = true)
    private String masterJobExecutor;
    @NacosValue(value = "${user.like.rates.base.data.range.days:3}", autoRefreshed = true)
    private Integer days;
    @NacosValue(value = "${targetActiveChatDurationMins:3}", autoRefreshed = true)
    private Integer targetActiveChatDurationMins;
    @NacosValue(value = "${refreshModelsLikesRatesData.job.switch:true}", autoRefreshed = true)
    private boolean refreshModelsLikesRatesDataJobSwitch;
    @NacosValue(value = "${refreshModelPool.job.switch:true}", autoRefreshed = true)
    private boolean refreshModelPoolJobSwitch;

    @NacosValue(value = "${refreshModelPool.onlineStatus.job.switch:true}", autoRefreshed = true)
    private boolean refreshModelPoolOnlineStatusJobSwitch;

    @NacosValue(value = "${task.job.executor.ip}", autoRefreshed = true)
    private String taskJobExecutor;

    //已解决跨天问题
    @Scheduled(fixedDelay = 1000 * 60 * 60)
    // TODO: 2024/1/12 可以考虑不全量更新，改成全量更新之后每个较小间隔更新这张表的逐条数据，因为这个表的有效数据很少，1000左右
    public void refreshModelsLikesRatesData() {
        long start = System.currentTimeMillis();
        log.info("==refreshModelsLikesRatesData 开始执行==");
        if (!refreshModelsLikesRatesDataJobSwitch) {
            log.info("==refreshModelsLikesRatesData 开关未打开==");
            return;
        }

        if (!taskJobExecutor.contains(localIP)) {
            log.info("=============refreshModelsLikesRatesData 当前机器不执行 NotTaskJobExecutor, currentLocalAddress:{}, onlyExecuteTaskIn:{}", localIP, masterJobExecutor);
            return;
        }

        statsDailyModelLikeRatesDao.refreshTodayData(days);
        log.info("==refreshModelsLikesRatesData 执行完成==,cost:{}", System.currentTimeMillis() - start);
    }

    @Scheduled(fixedDelay = 1000 * 60)
    public void refreshModelPool() {
        long start = System.currentTimeMillis();
        log.info("==refreshModelPool 开始执行==");

        if (!refreshModelPoolJobSwitch) {
            log.info("==refreshModelPool 开关未打开==");
            return;
        }

        if (!masterJobExecutor.contains(localIP)) {
            log.info("=============refreshModelPool 当前机器不执行 NotTaskJobExecutor, currentLocalAddress:{}, onlyExecuteTaskIn:{}", localIP, masterJobExecutor);
            return;
        }

        LocalDateTime now = LocalDateTime.now();
        LocalDateTime targetMins = now.minusMinutes(targetActiveChatDurationMins);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = targetMins.format(formatter);
//        long start = System.currentTimeMillis();
        log.info("==refreshModelPool 开始更新pool==");
        trafficModelPoolDao.refreshAllData(format);
        log.info("==refreshModelPool 开始更新pool 结束==,cost:{}", System.currentTimeMillis() - start);

        trafficModelPoolDao.removeOldData();
        log.info("==refreshModelPool 开始更新pool 移除低版本数据==,cost:{}", System.currentTimeMillis() - start);

        log.info("==refreshModelPool 执行完成==,cost:{}", System.currentTimeMillis() - start);
    }

    @Scheduled(fixedDelay = 1000 * 20)
    public void updatePoolOnlineStatus() {
        long start = System.currentTimeMillis();
        log.info("==updatePoolOnlineStatus 开始执行==");

        if (!refreshModelPoolOnlineStatusJobSwitch) {
            log.info("==updatePoolOnlineStatus 开关未打开==");
            return;
        }

        if (!masterJobExecutor.contains(localIP)) {
            log.info("=============updatePoolOnlineStatus 当前机器不执行 NotTaskJobExecutor, currentLocalAddress:{}, onlyExecuteTaskIn:{}", localIP, masterJobExecutor);
            return;
        }

        trafficModelPoolDao.updateOnlineStatus();
        log.info("==updatePoolOnlineStatus 完成==,cost:{}", System.currentTimeMillis() - start);
    }

//    @Scheduled(fixedDelay = 1000 * 20)
    public void updateActiveChatsOfPool() {
        long start = System.currentTimeMillis();
        log.info("==updateActiveChatsOfPool 开始执行==");

        if (!refreshModelPoolOnlineStatusJobSwitch) {
            log.info("==updateActiveChatsOfPool 开关未打开==");
            return;
        }

        if (!masterJobExecutor.contains(localIP)) {
            log.info("=============updateActiveChatsOfPool 当前机器不执行 NotTaskJobExecutor, currentLocalAddress:{}, onlyExecuteTaskIn:{}", localIP, masterJobExecutor);
            return;
        }


        LocalDateTime now = LocalDateTime.now();
        LocalDateTime targetMins = now.minusMinutes(targetActiveChatDurationMins);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = targetMins.format(formatter);
//        trafficModelPoolDao.updateActiveChats(format,1);
//        trafficModelPoolDao.updateActiveChats(format,2);
        log.info("==updateActiveChatsOfPool 完成==,cost:{}", System.currentTimeMillis() - start);
    }

}
